package com.xiaoyu.izf.controller.api;

import com.xiaoyu.izf.config.MyConfig;
import com.xiaoyu.izf.controller.BaseController;
import com.xiaoyu.izf.model.NewsInfo;
import com.xiaoyu.izf.model.page.FormResult;
import com.xiaoyu.izf.service.NewsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/api/news")
public class NewsApiController extends BaseController {
    @Autowired
    private MyConfig myConfig;
    @Autowired
    private NewsInfoService newsInfoService;

    @RequestMapping("/list")
    @ResponseBody
    public FormResult<Page<NewsInfo>> list(int p, int psize){
        FormResult<Page<NewsInfo>> fr = new FormResult<>();
        Page<NewsInfo> pagelist = newsInfoService.listForApi(p,psize);
        List<NewsInfo> list = pagelist.getContent();
        if(list!=null&&list.size()>0){
            fr.setStatus(200);
            fr.setMessage("ok");
            List<NewsInfo> list1 = new ArrayList<>();
            for (NewsInfo bean:list){
                bean.setDetail("");
                //bean.setNewsUrl(myConfig.getHost()+"api/news/one?id="+bean.getId());
                //bean.setNewsUrl("https://www.aizhifupay.cn/wap/pages/news/news_"+bean.getId()+".html");
                list1.add(bean);
            }

            fr.setData(pagelist);
        }else{
            fr.setStatus(404);
            fr.setMessage("没有数据。");
        }
        return fr;
    }

    @RequestMapping("/one")
    public String newOne(int id, Model model){
        NewsInfo newsInfo = newsInfoService.newsOne(id);
        model.addAttribute("news",newsInfo);
        return "/news/news_one";
    }
}
